package com.lbm.baseclass.controller;

import java.io.IOException;
import java.sql.Clob;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialClob;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.lbm.baseclass.constants.Consts;
import com.lbm.baseclass.service.IBaseService;
import com.lbm.baseclass.utils.json.JsonUtil;
import com.lbm.model.UserInfo;

public class BaseController extends MultiActionController {
	protected final Logger log = Logger.getLogger(this.getClass());
	protected final Map<String, Object> json = new HashMap<String, Object>();
	protected <T> List<T> calcPage(HttpServletRequest request,
			IBaseService service, String hql) {
		String pageSize = request.getParameter("rows");
		String pageNo = request.getParameter("page");
		if (pageSize == null)
			pageSize = "15";
		if (pageNo == null)
			pageNo = "1";
		int total = service.getDataTotalNum(hql);
		int rows = Integer.parseInt(pageSize);
		int pages = total % rows == 0 ? (total / rows) : (total / rows + 1);
		request.setAttribute("rows", rows);
		request.setAttribute("total", total);
		request.setAttribute("page", Integer.parseInt(pageNo));
		request.setAttribute("pages", pages);
		@SuppressWarnings("unchecked")
		List<T> list = service.getPageDataByHQL(hql,
				Integer.parseInt(pageSize), Integer.parseInt(pageNo));
		return list;
	}
	public List<Map<String, Object>> calcSqlPage(HttpServletRequest request, IBaseService svc, String sql) {
		String pageSize = request.getParameter("rows");
		String pageNo = request.getParameter("page");
		if (pageSize == null)
			pageSize = "15";
		if (pageNo == null)
			pageNo = "1";
		int total = (int) svc.getTotalNumBySQL(sql);
		int rows = Integer.parseInt(pageSize);
		int pages = total % rows == 0 ? (total / rows) : (total / rows + 1);
		int page = Integer.parseInt(pageNo);
		if (page > pages) page = 1;
		List<Map<String, Object>> list = svc.queryForPagedListBySQL(sql, page, rows);
		request.setAttribute("rows", rows);
		request.setAttribute("total", total);
		request.setAttribute("page", page);
		request.setAttribute("pages", pages);
		return list;
	}
	public void renderJson(final HttpServletResponse response,
			final String content) {
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(content);
		} catch (final IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
	protected void putToJson(String name, Object value) {
		json.put(name, value);
	}
	public String toJson(String key, Object value) {
		JSONObject json = new JSONObject();
		json.put(key, value);
		return JsonUtil.toString(json);
	}
	public String toJsArray(String... vals) {
		JSONArray arr = JSONArray.fromObject(vals);
		return JsonUtil.toString(arr);
	}
	protected String getJsonAndReset(String... excludePropertis) {
		String str = JsonUtil.toString(json, excludePropertis);
		json.clear();
		return str;
	}
	public void putSession(final HttpServletRequest request,
			final String attributeKey, final Object obj) {
		request.getSession().setAttribute(attributeKey, obj);
	}
	protected UserInfo getUser(HttpServletRequest request) {
		UserInfo user = (UserInfo) request.getSession().getAttribute(
				Consts.USER_INFO);
		return user;
	}
	protected String getUserId(HttpServletRequest request) {
		return (String) request.getSession().getAttribute(
				Consts.USER_ID);
	}
	protected Clob getUeditorContents(String contents) throws Exception{
		Clob c = new SerialClob(java.net.URLDecoder.decode(contents, "UTF-8").toCharArray());
		return c;
	}
}
